//
//  YSPhotoBrowserViewController.m
//  YSKitDemo
//
//  Created by Kyson on 2018/9/19.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSPhotoBrowserViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "YSPhotoBrowser.h"

@interface YSPhotoBrowserViewController ()
@property (nonatomic, strong) NSArray *urls;
@end

@implementation YSPhotoBrowserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _urls =  @[
               @"http://ww2.sinaimg.cn/thumbnail/642beb18gw1ep3629gfm0g206o050b2a.gif",
               @"http://ww4.sinaimg.cn/thumbnail/9e9cb0c9jw1ep7nlyu8waj20c80kptae.jpg",
               @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr1xydcj20gy0o9q6s.jpg",
               @"http://ww2.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr2n1jjj20gy0o9tcc.jpg",
               @"http://ww4.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr4nndfj20gy0o9q6i.jpg",
               @"http://ww3.sinaimg.cn/thumbnail/8e88b0c1gw1e9lpr57tn9j20gy0obn0f.jpg",
               @"http://ww2.sinaimg.cn/thumbnail/677febf5gw1erma104rhyj20k03dz16y.jpg",
               @"http://ww4.sinaimg.cn/thumbnail/677febf5gw1erma1g5xd0j20k0esa7wj.jpg",
               @"http://ww4.sinaimg.cn/thumbnail/a15bd3a5jw1f12r9ku6wjj20u00mhn22.jpg",
               ];
    
    
    CGFloat y = 100;
    CGFloat w = 100;
    CGFloat h = 100;
    CGFloat x = 20;
    CGFloat space = (self.view.bounds.size.width - w * 3 - x * 2) / 2;
    
    for (int i = 0; i < _urls.count; i++) {
        UIImageView *imgv = [[UIImageView alloc] init];
        imgv.frame = CGRectMake(x + (w + space) * (i%3), y + (h + 20)*(i/3), w, h);
        imgv.tag = i+100;
        [imgv sd_setImageWithURL:[NSURL URLWithString:_urls[i]]];
        [imgv addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImgv:)]];
        imgv.backgroundColor = [UIColor ys_randomColor];
        imgv.userInteractionEnabled = YES;
        [self.view addSubview:imgv];
    }
}

- (void)tapImgv:(UITapGestureRecognizer *)tap {
    NSMutableArray *items = @[].mutableCopy;
    for (int i = 0; i < _urls.count; i++) {
        UIImageView *imgv = [self.view viewWithTag:i+100];
        NSString *url = [_urls[i] stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
        YSPhotoItem *item = [YSPhotoItem itemWithSourceView:imgv imageUrl:[NSURL URLWithString:url]];
        [items addObject:item];
    }
    YSPhotoBrowser *browser = [YSPhotoBrowser browserWithPhotoItems:items selectedIndex:tap.view.tag-100];
//    browser.delegate = self;
    [browser showFromViewController:self];
}

@end
